package com.google.code.jcooky.rpc.connect;

import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;

public class HttpConnectorOutputStream extends OutputStream {
	
	private HttpURLConnection conn;
	private StringBuffer strbuf = new StringBuffer(1024);
	
	public HttpConnectorOutputStream(HttpURLConnection conn) {
		this.conn = conn;
	}

	@Override
	public void write(int i) throws IOException {
		throw new UnsupportedOperationException();
	}
	
	@Override
	public void write(byte []b) throws IOException {
		strbuf.append(new String(b, "UTF-8"));
	}

	@Override
	public void close() throws IOException {
		String jsonString = strbuf.toString();
		synchronized(conn) {
			conn.addRequestProperty("req_json", jsonString);
		}
		
		super.close();
	}
}
